package eu.webhib.frmwrk.utils;

//
import java.util.*;
import org.apache.log4j.Logger;

//
public class ExtendedProperties extends Properties {

   private static final Logger log = Logger.getLogger(ExtendedProperties.class);
   private static final long serialVersionUID = 1L;

   public String get(String key) {
      assert (!isEmpty());
      return getProperty(key);
   }

   public int getInt(String key) {
      return getInt(key, null);
   }

   public double getDouble(String key) {
      return getDouble(key, null);
   }

   public boolean getBoolean(String key) {
      return getBoolean(key, null);
   }

   public int getInt(String key, Integer defValue) {
      String value = get(key);
      if (value == null) {
         if (defValue != null) {
            return defValue;
         } else {
            log.error("Property not found");
            return 0;
         }
      }
      try {
         return Integer.parseInt(value);
      } catch (Throwable t) {
         log.error("Invalid property value format for: " + key, t);
         return defValue != null ? defValue : 0;
      }
   }

   public double getDouble(String key, Double defValue) {
      String value = get(key);
      if (value == null) {
         if (defValue != null) {
            return defValue;
         } else {
            log.error("Property not found");
            return 0;
         }
      }
      try {
         return Double.parseDouble(value);
      } catch (Throwable t) {
         log.error("Invalid property value format for: " + key, t);
         return defValue != null ? defValue : 0;
      }
   }

   public boolean getBoolean(String key, Boolean defValue) {
      String value = get(key);
      if (value == null) {
         if (defValue != null) {
            return defValue;
         } else {
            log.error("Property not found");
            return false;
         }
      }
      try {
         return Boolean.parseBoolean(value);
      } catch (Throwable t) {
         log.error("Invalid property value format for: " + key, t);
         return defValue != null ? defValue : false;
      }
   }
}
